/* 
Copyright (c) 2004,2005, 2006, 2007, 2008 University of Illinois 

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
AccessibilityExtension.ns(function() { with (AccessibilityExtension.LIB) {

	var AE = AccessibilityExtension.ns;  
	AE.dialogName = "accessext-form-list-textsizing";	

	function textsizingObjectDisplayDetails() {}
	textsizingObjectDisplayDetails.prototype = {
		dataElementNames : ['icita07', 'icita08','order', 'baseuri','style', 'lineno', 'fromfile'],
		columnHeaders : ['column.header.icita07.label', 'column.header.icita08.label','column.header.order.label','column.header.baseuri.label','column.header.style.label','column.header.lineno.label','column.header.fromfile.label'],
		
		accessIssuesDataElementNames : ['absolute'],
		accessIssuesMessages : ['textsizing.msg.absolute'],
	}
	var textsizingObjectDisplayDetails = new textsizingObjectDisplayDetails();
	
	function textsizingObject(torder, tnode, art) {
		this.order = torder;
		if (art != '') {
			this.node = tnode.parentNode;
			this.style = art;		
		} else {
			if (tnode != null) {this.parseNode(tnode);}	
		}
	
	}
	textsizingObject.prototype = {
		icita07 : "",
		icita08 : "",
		node : null,
		order : 0,
		baseuri : '',
		style : "",
		fromfile : "",
		lineno: "",
		absolute : false,
		display : true,
		displayDetails: textsizingObjectDisplayDetails,
		parseNode : function(node) {
			try {  // from the accessibility Issues Dialog
				this.fromfile = 'inline';
				this.node = node;
				this.baseuri = getBaseURI(node);
				if (node.nodeName.toLowerCase() == 'style') {
					this.style = node.textContent;
				} else {
					this.style = node.getAttribute("style");				
				}
				// reports everything in pt regardless of what is was in the source code
				//var fontSize = mydoc.defaultView.getComputedStyle(node, null).getPropertyValue("font-size");
				var fontSize = this.style.toLowerCase();
				if (fontSize.indexOf("font-size") >= 0) {
					fontSize = fontSize.substring(fontSize.indexOf("font-size"));
					if (fontSize.indexOf(";") >= 0) { fontSize = fontSize.substring(0, fontSize.indexOf(";"));}
					if ((fontSize.indexOf('pt') >= 0) || (fontSize.indexOf('px') >= 0)  || (fontSize.indexOf('in') >= 0) || (fontSize.indexOf('cm') >= 0)  || (fontSize.indexOf('mm') >= 0) ) {
						this.absolute = true;
					}
				} 
				//this.style = this.style  +  ' ' + mydoc.defaultView.getComputedStyle(node, null).getPropertyValue("font-size");
			} catch(e) { 
				accessext_consoleDump('textsizing parseNode exception: ', ex);
			}
		},
		getTreeNode : function(windoc) {
			return accesxt_generateTreeboxResults(windoc,this);
		}
	}
AccessibilityExtension.LIB.extend(AccessibilityExtension.LIB,
{
	post_InitAccessibilityExt: function() {
		try {
	//		treebox.addEventListener('keypress',accessext_langSearchOnKeyPress,true);
		} catch (ex) {
			accessext_consoleDump('Failed to add onkeypress event handler: ' + ex);
		}
		accessext_setNoteText(0);
	},
	
	ParseDocument: function(doc) {
		ParseDocumentForTextSizing(doc, AE.dataarray);
	},
	
	ParseDocumentForTextSizing: function(doc, srcarray) {
		try {
			var myCSSParser = newCSSParser("all", doc); 
			//ReturnText("");
			for(var i=0; i<myCSSParser.fontsize.length-1; i++) {
				AE.dataarray[AE.dataarray.length] = new textsizingObject(AE.dataarray.length + 1,null, '');
				AE.dataarray[AE.dataarray.length-1].style = myCSSParser.fontsize[i];
				AE.dataarray[AE.dataarray.length-1].fromfile = myCSSParser.filename[i];
				AE.dataarray[AE.dataarray.length-1].lineno = myCSSParser.fileLineNo[i];
				if (myCSSParser.fontsize[i].indexOf('pt}') >=0 ) {AE.dataarray[AE.dataarray.length-1].absolute = true; }
				if (myCSSParser.fontsize[i].indexOf('in}') >=0 ) {AE.dataarray[AE.dataarray.length-1].absolute = true; }
				if (myCSSParser.fontsize[i].indexOf('cm}') >=0 ) {AE.dataarray[AE.dataarray.length-1].absolute = true; }
				if (myCSSParser.fontsize[i].indexOf('mm}') >=0 ) {AE.dataarray[AE.dataarray.length-1].absolute = true; }
				if (myCSSParser.fontsize[i].indexOf('px}') >=0 ) {AE.dataarray[AE.dataarray.length-1].absolute = true; }		
			}
			
			var xpathResult = doc.evaluate("//*[@style]", doc,	null, XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);
			var r = xpathResult.iterateNext();
			while (r) {
				try {  // from the accessibility Issues Dialog
					var style = r.getAttribute("style");
					// reports everything in pt reguardless of what is was in the source code
					//var fontSize = mydoc.defaultView.getComputedStyle(node, null).getPropertyValue("font-size");
					var fontSize = style.toLowerCase();
					if (fontSize.indexOf("font-size") >= 0) {
						srcarray[srcarray.length] = new textsizingObject(srcarray.length + 1,r, '');
					} 
					r = xpathResult.iterateNext();
				} catch(e) { 
					accessext_consoleDump('textsizing parseNode exception: ', e);
				}
			}
			var xpathResult = doc.evaluate("//style", doc,	null, XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);
			var r = xpathResult.iterateNext();
			while (r) {
				try {  // from the accessibility Issues Dialog
					var style = r.textContent;
					// reports everything in pt reguardless of what is was in the source code
					//var fontSize = mydoc.defaultView.getComputedStyle(node, null).getPropertyValue("font-size");
					var fontSize = style.toLowerCase();
					if (fontSize.indexOf("font-size") >= 0) {
						srcarray[srcarray.length] = new textsizingObject(srcarray.length + 1,r, '');
					} 
					r = xpathResult.iterateNext();
				} catch(e) { 
					accessext_consoleDump('textsizing parseNode exception: ', e);
				}
			}
	
		//	ParseDocumentForAsciiArt(doc, AE.dataarray);
			setMaxSeverityForArray(srcarray);
		} catch (ex) {
			accessext_consoleDump('ParseDocumentForTextSizing exception: ' + ex);
		}
	},
	
	AsciiArt: function(node) 
	{
		try {  // from the accessibility Issues Dialog
			var art = '';
		    
			if ((node.nodeType != node.TEXT_NODE) || (node.parentNode.nodeName.toUpperCase() == 'SCRIPT') || (node.parentNode.nodeName.toUpperCase() == 'STYLE')) {
	          return art;
			}
			var text = node.textContent; //getDisplayableTextRecursively(node);
			
			// this will not pick up emoticons with spaces in them : )
			for(var i=0; i<text.length-1; i++) {
				var currChar = text.charCodeAt(i)
				//below space, period, '0 - 9', 'A - Z', 'a - z'
				if ((currChar <= 32) || (currChar == 46) || (currChar == 160) ||
				(currChar >= 48 && currChar <= 57) ||
				(currChar >= 65 && currChar <= 90) ||
				(currChar >= 97 && currChar <= 122) ) {
					//nothing
				} else {
					var nextChar = text.charCodeAt(i + 1);
					if ((nextChar <= 32) || (nextChar == 46) || (nextChar == 160) ||
					(nextChar >= 48 && nextChar <= 57) ||
					(nextChar >= 65 && nextChar <= 90) ||
					(nextChar >= 97 && nextChar <= 122) ) {
						i++;
					} else { // found 2 nonalphanumerics in a row
						if (text[i] != text[i+1]) { //especially >>, //
							if (i == 1) {
								art = text[i] + text[i+1] + ' (' + currChar + ',' + nextChar + ' ) is potentially ascii art';
							} else if ((text.charCodeAt(i - 1) <= 32) || (text.charCodeAt(i - 1) == 160)) {
								art = text[i] + text[i+1] + ' (' + currChar + ',' + nextChar + ' ) is potentially ascii art';
							}				
						}
					}
				}
			}
			return art;
		} catch (ex) {
			accessext_consoleDump('AsciiArt exception: ' + ex);
		}
	},
	
	ParseDocumentForAsciiArt: function(node, srcarray) {
		try {  // from the accessibility Issues Dialog
			var i;
			var found = false;
			var art = AsciiArt(node);
			if(art != '') {
				srcarray[srcarray.length] = new textsizingObject(srcarray.length + 1,node, art);
			}
		
			if(node.childNodes) {
				for(var i = 0; i < node.childNodes.length; i ++)
					ParseDocumentForAsciiArt(node.childNodes[i], srcarray);
			}
		} catch (ex) {
			accessext_consoleDump('ParseDocumentForAsciiArt exception: ' + ex);
		}
	},
	
	accessext_handle_treebox_textsizing: function() {
		try {
			if (AE.treebox.currentIndex < 0 || AE.treebox.currentIndex >= AE.dataarray.length) {
				return;
			}
			
			var findex = AE.treebox.currentIndex;
			var node = AE.dataarray[findex].node;
			if(node != null && mydoc != null) {
				accessext_treeboxScrollAndHighlightNode(); //accessext_scrollAndHighlightNode(node);
			}
		
			accessext_setNoteText(findex);	
		} catch (ex) {
			accessext_consoleDump('accessext_handle_treebox exception ' + ex + '(' +  findex + ')');
		}
	},
	
	focusOnActive: function() {
		CleanUp();
		var theIndex = accessext_getActiveValue(AE.treebox);
		if (theIndex < 0 || theIndex >= AE.dataarray.length  || theIndex == null) {
			return false;
		}
		var node = AE.dataarray[theIndex].node;
		accessext_keyboardFocusNode(node,AE.thewindow);
		return true;
	},
	
	RemoveRelative: function() {
		var i;
		var elem = document.getElementById("checkbox-remove-relative");
		for(i = 0; i < AE.dataarray.length; i ++) {
			//Instead of removing, just hide it
			if(elem.checked && AE.dataarray[i].absolute == false) { AE.dataarray[i].display = false; }
			else {AE.dataarray[i].display = true;}
		}
		UpdateTreeView();
	}
});
	if (AccessibilityExtension.LIB.InitAccessibilityExt) AccessibilityExtension.LIB.InitAccessibilityExt();	
}});